package org.example.innovate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.innovate.common.CommonResponse;
import org.example.innovate.domain.Collections;
import org.example.innovate.domain.Dish;
import org.example.innovate.domain.User;
import org.example.innovate.mapper.DishMapper;
import org.example.innovate.mapper.UserMapper;
import org.example.innovate.service.CollectionService;
import org.example.innovate.mapper.CollectionMapper;
import org.example.innovate.util.JwtUtil;
import org.example.innovate.vo.DishOneVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【collection】的数据库操作Service实现
* @createDate 2025-03-13 17:01:14
*/
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collections>
    implements CollectionService{

    @Resource
    private CollectionMapper collectionMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private UserMapper userMapper;

    public CommonResponse<List<DishOneVo>> getAll(HttpServletRequest request) {
        String token=request.getHeader("token");
        System.out.println(token);
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户不存在");
        }
        QueryWrapper<Collections> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        List<Collections> list=collectionMapper.selectList(queryWrapper);
        List<Dish> dishes=new ArrayList<>();
        for(Collections collections:list)
        {
            QueryWrapper<Dish> dishQueryWrapper=new QueryWrapper<>();
            dishQueryWrapper.eq("dish_id", collections.getPostId());
            Dish dish=dishMapper.selectOne(dishQueryWrapper);
            dishes.add(dish);
        }
        List<DishOneVo> dishOneVos=new ArrayList<>();
        for(Dish dish:dishes)
        {
            QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
            userQueryWrapper.eq("user_id",dish.getUserId());
            user=userMapper.selectOne(userQueryWrapper);
            DishOneVo dishOneVo=new DishOneVo();
            dishOneVo.setUsername(user.getUsername());
            Calendar cal = Calendar.getInstance();
            cal.setTime(dish.getCreatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            Date eightHoursAgo = cal.getTime();
            dish.setCreatedAt(eightHoursAgo);
            cal.setTime(dish.getUpdatedAt());
            cal.add(Calendar.HOUR_OF_DAY, 8);
            eightHoursAgo = cal.getTime();
            dish.setUpdatedAt(eightHoursAgo);
            dishOneVo.setDish(dish);
            dishOneVos.add(dishOneVo);
        }
        return CommonResponse.createForSuccess("查询成功",dishOneVos);
    }
}




